package com.boc.ewr.business.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.boc.ewr.business.entity.CaUncaAll;
import com.boc.ewr.business.entity.vo.CaUncaAllRespVo;
import com.boc.ewr.business.service.ICaUncaAllService;
import com.boc.ewr.util.controller.ControllerCus;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.Set;

/**
 * @Description: 未对账数据汇总表
 * @Author: jeecg-boot
 * @Date:   2023-04-20
 * @Version: V1.0
 */
@Api(tags="未对账数据汇总表")
@RestController
@RequestMapping("/business/caUncaAll")
@Slf4j
public class CaUncaAllController extends ControllerCus<CaUncaAll, ICaUncaAllService> {
	@Autowired
	private ICaUncaAllService caUncaAllService;
	 @Autowired
	 private ISysBaseAPI sysBaseAPI;

	/**
	 * 分页列表查询
	 *
	 * @param caUncaAll
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "未对账数据汇总表-分页列表查询")
	@ApiOperation(value="未对账数据汇总表-分页列表查询", notes="未对账数据汇总表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<CaUncaAll>> queryPageList(CaUncaAll caUncaAll,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<CaUncaAll> queryWrapper = QueryGenerator.initQueryWrapper(caUncaAll, req.getParameterMap());
		Set<String> orgCodeSetByLoginUser = sysBaseAPI.getOrgCodeSetByLoginUser(this.getLoginUser());
		if (orgCodeSetByLoginUser!=null) {
			queryWrapper.in("obranch_no", orgCodeSetByLoginUser);
		}
		Page<CaUncaAll> page = new Page<CaUncaAll>(pageNo, pageSize);
		IPage<CaUncaAll> pageList = caUncaAllService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param caUncaAll
	 * @return
	 */
	@AutoLog(value = "未对账数据汇总表-添加")
	@ApiOperation(value="未对账数据汇总表-添加", notes="未对账数据汇总表-添加")
	//@RequiresPermissions("com.boc.ewr:ca_unca_all:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody CaUncaAll caUncaAll) {
		caUncaAllService.save(caUncaAll);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param caUncaAll
	 * @return
	 */
	@AutoLog(value = "未对账数据汇总表-编辑")
	@ApiOperation(value="未对账数据汇总表-编辑", notes="未对账数据汇总表-编辑")
	//@RequiresPermissions("com.boc.ewr:ca_unca_all:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody CaUncaAll caUncaAll) {
		caUncaAllService.updateById(caUncaAll);
		return Result.OK("编辑成功!");
	}

	 /**
	  *  反馈，后台自动赋值反馈人，反馈时间
	  *
	  * @param caUncaAll
	  * @return
	  */
	 @AutoLog(value = "未对账数据汇总表-反馈")
	 @ApiOperation(value="未对账数据汇总表-反馈", notes="未对账数据汇总表-反馈")
	 //@RequiresPermissions("com.boc.ewr:ca_unca_all:fdbk")
	 @RequestMapping(value = "/fdbk", method = {RequestMethod.PUT,RequestMethod.POST})
	 public Result<String> fdbk(@RequestBody CaUncaAll caUncaAll) {
		 //获取反馈人和反馈时间
		 LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 caUncaAll.setFdbkPsn(user.getRealname());
		 caUncaAll.setFdbkTime(DateUtils.getDate());
		 caUncaAllService.updateById(caUncaAll);
		 return Result.OK("反馈成功!");
	 }

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "未对账数据汇总表-通过id删除")
	@ApiOperation(value="未对账数据汇总表-通过id删除", notes="未对账数据汇总表-通过id删除")
	//@RequiresPermissions("com.boc.ewr:ca_unca_all:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		caUncaAllService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "未对账数据汇总表-批量删除")
	@ApiOperation(value="未对账数据汇总表-批量删除", notes="未对账数据汇总表-批量删除")
	//@RequiresPermissions("com.boc.ewr:ca_unca_all:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.caUncaAllService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "未对账数据汇总表-通过id查询")
	@ApiOperation(value="未对账数据汇总表-通过id查询", notes="未对账数据汇总表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<CaUncaAll> queryById(@RequestParam(name="id",required=true) String id) {
		CaUncaAll caUncaAll = caUncaAllService.getById(id);
		if(caUncaAll==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(caUncaAll);
	}

    /**
    * 导出Txt
    *
    * @param request
    * @param caUncaAll
    */
	 @RequestMapping(value = "/exportTxt", method = RequestMethod.POST)
	 //@RequiresPermissions("com.boc.ewr:ca_unca_all:exportTxt")
	 public void exportTxt(HttpServletRequest request, HttpServletResponse response, CaUncaAll caUncaAll) throws IOException {
		 //生成文件，并下载
		 caUncaAllService.exportTxt(request, response, caUncaAll);
	 }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("ca_unca_all:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, CaUncaAll.class);
    }

	 private LoginUser getLoginUser() {
		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 return sysUser;
	 }
}
